/*
 * @Description:
 * @Author: xuzp4
 * @Date: 2022-03-22 08:54:13
 * @LastEditors: xuzp4
 * @LastEditTime: 2022-08-15 21:41:28
 * @FilePath: \visitor_wechat\src\services\index.js
 */
import axios from 'axios'
import store from '@/store/index'
import i18n from '@/lang/index'
import router from '@/router/index'
import {
  Message,
  Loading
} from 'element-ui';
/** 员工接口 */

// 员工——接口请求统一封装
/**
 * 
 * @param {} url 
 * @param {get,post,postquery} method 
 * @param {参数} params 
 * @returns 
 */
export default function httpRequest({
  url,
  method = 'post',
  params = {},
  headers = {},
  ...rest
}) {
  const tokenVal = store.state.token
  // const sign = signMd5Utils.getSign(url, params)
  const configParam = {
    url,
    method,
    headers: {
      Sessionid: tokenVal,
      ...headers
    },
    ...rest
  }
  if (method.toLowerCase() === 'get') {
    configParam.params = params
  } else if (method.toLowerCase() === 'postquery') {
    configParam.method = 'post'
    configParam.params = params
  } else {
    configParam.data = params;
  }
  let loading;
  if (configParam.isLoading !== false) {
    loading = Loading.service({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.2)'
    });
  }
  // axios.defaults.baseURL = 'http://192.168.5.222/';
  return axios(configParam).then(res => {
    if (res.data.code === 200) {
      return res.data
    } else {
      notifyError(res.data)
      throw res.data
    }
  }).catch(error => {
    throw error
  }).finally(() => {
    loading && loading.close()
  })
}

function notifyError(res) {
  if (res.code === 400) return;
  if (res.data && typeof res.data.retry_times == 'number') {
    Message({
      message: i18n.t('message.' + res.code, [res.data.retry_times]),
      type: 'error'
    })
  } else {
    Message({
      message: i18n.t('message.' + res.code),
      type: 'error'
    })
  }
  if (res.code === -141) {
    store.dispatch('resetInfo')
    router.push('/login')
  }

}

export function getGroupInfo(params = {}) {
  params = {
    ...params,
    msg_id: '262'
  }
  return requestJson(params)
}

export function getCamera(config) {
  const params = {
    msg_id: '516'
  }
  return requestJson(params,config)
}
// 本地跨域调试需要传sessionid,同域调试只要cookie即可,cookie不用主动传,后面如果是同域,此方法改为promise返回url即可
export function getImgUrl(url) {
  return window.config.resourceHost + url
  // return window.config.resouceHost + url;
  // const tokenVal = store.state.token
  // return axios({
  //   method: 'get',
  //   url: window.config.resourceHost + url,
  //   responseType: 'blob', // 设置响应类型为二进制数据
  //   headers: {
  //     Sessionid: tokenVal,
  //   },
  // }).then(res => {
  //   return URL.createObjectURL(res.data);
  // })
}

export function imageUrlToFile(url) {
  const tokenVal = store.state.token
  return axios({
    method: 'get',
    url: window.config.resourceHost + url,
    responseType: 'blob', // 设置响应类型为二进制数据
    headers: {
      Sessionid: tokenVal,
    },
  }).then(res => {
    return res.data;
  })
}

export function validatePwd(params) {
  return requestJson({
    ...params,
    msg_id: '271'
  })
}

export async function downLoadUrl(url) {
  let fileUrl = getImgUrl(url)
  const a = document.createElement('a')
  a.href = fileUrl
  a.download = ''
  a.click()
}

export function importFile(formData) {
  const params = new FormData()
  for (let key in formData) {
    params.append(key, formData[key])
  }
  return httpRequest({
    url: window.config.formHost,
    method: 'post',
    params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function requestJson(formData,config) {
  return httpRequest({
    url: window.config.jsonHost,
    method: "post",
    params: {
      ...formData,
    },
    ...config
  });
}

export function getDetailInfo(formData) {
  return requestJson({
    ...formData,
    msg_id: '1067'
  })
}

export function getAuth(formData) {
  return requestJson({
    ...formData,
    msg_id: '270'
  })
}
export function setAuth(formData) {
  return requestJson({
    ...formData,
    msg_id: '269'
  })
}

export function getAllLib() {
  return requestJson({
    msg_id: '1028'
  }).then(res => {
    if (res.code == 0) {
      res.data = res.data.map(item => {
        return {
          ...item,
          lib_id: item.lib_id.toString()
        }
      })
    }
    return res;
  })
}